

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/fluid.png">
  <link rel="icon" href="/img/fluid.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="深海火锅店">
  <meta name="keywords" content="">
  
    <meta name="description" content="简单的SELECT语句语法1SELECT [ * |ALL | DISTINCT column1, column2, ... ] FROM table_name[,table_name2,...];   星号(*)表示输出结果里包含表里的全部字段 选项ALL用于显示一列的全部值，包括重复值。选项ALL是默认的操作方式，这意味着它并不在SELECT语句中明确指定。 选项DISTINCT禁止在输出结果">
<meta property="og:type" content="article">
<meta property="og:title" content="DB08_数据库查询">
<meta property="og:url" content="http://example.com/2022/12/22/%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%AD%A6%E4%B9%A0/DB08&DB09/DB08_%E6%95%B0%E6%8D%AE%E5%BA%93%E6%9F%A5%E8%AF%A2/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="简单的SELECT语句语法1SELECT [ * |ALL | DISTINCT column1, column2, ... ] FROM table_name[,table_name2,...];   星号(*)表示输出结果里包含表里的全部字段 选项ALL用于显示一列的全部值，包括重复值。选项ALL是默认的操作方式，这意味着它并不在SELECT语句中明确指定。 选项DISTINCT禁止在输出结果">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-12-22T12:40:00.000Z">
<meta property="article:modified_time" content="2022-12-31T08:12:50.000Z">
<meta property="article:author" content="深海火锅店">
<meta property="article:tag" content="大数据">
<meta name="twitter:card" content="summary_large_image">
  
  
    <meta name="referrer" content="no-referrer-when-downgrade">
  
  
  <title>DB08_数据库查询 - Hexo</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.9.3","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"follow_dnt":true,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
  


  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fluid</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/img/background.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="DB08_数据库查询"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2022-12-22 20:40" pubdate>
          2022年12月22日 晚上
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          3.6k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          31 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">DB08_数据库查询</h1>
            
            
              <div class="markdown-body">
                
                <h4 id="简单的SELECT语句语法"><a href="#简单的SELECT语句语法" class="headerlink" title="简单的SELECT语句语法"></a>简单的SELECT语句语法</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> [ <span class="hljs-operator">*</span> <span class="hljs-operator">|</span><span class="hljs-keyword">ALL</span> <span class="hljs-operator">|</span> <span class="hljs-keyword">DISTINCT</span> column1, column2, ... ] <span class="hljs-keyword">FROM</span> table_name[,table_name2,...];<br></code></pre></td></tr></table></figure>

<ul>
<li>星号(*)表示输出结果里包含表里的全部字段</li>
<li>选项ALL用于显示一列的全部值，包括重复值。选项ALL是默认的操作方式，这意味着它并不在SELECT语句中明确指定。</li>
<li>选项DISTINCT禁止在输出结果里包含重复的行。</li>
</ul>
<h4 id="FROM子句"><a href="#FROM子句" class="headerlink" title="FROM子句"></a>FROM子句</h4><p>FROM子句必须与SLELCT语句联合使用，它是任何查询的重要元素，其作用是告诉数据库从哪些表里获取所需的数据，它可以指定一个或多个表，且必须至少指定一个表。<br>但是…对于MySQL来说，FROM在一些情况是可以忽略，如下:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">select</span> version() <span class="hljs-keyword">as</span> 数据库版本,now() <span class="hljs-keyword">as</span> 当前时间,<span class="hljs-keyword">user</span>() <span class="hljs-keyword">as</span> 登录用户;<br></code></pre></td></tr></table></figure>

<h4 id="WHERE子句"><a href="#WHERE子句" class="headerlink" title="WHERE子句"></a>WHERE子句</h4><p>查询里的条件指定了要返回满足什么标准的信息。条件的值是TRUE或FALSE，从而限制查询中获取的数据。WHERE子句用于给查询添加条件，从而去除用户不需要的数据。<br>WHERE子句里可以有多个条件，<code>它们之间以操作符AND或OR连接</code>，具体的讲解和实际应用在后面章节进行。<br>操作符是SQL里的字符或关键字，用于连接SQL语句里的元素。WHERE子句的语法如下所示:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> [ <span class="hljs-operator">*</span> <span class="hljs-operator">|</span><span class="hljs-keyword">ALL</span> <span class="hljs-operator">|</span> <span class="hljs-keyword">DISTINCT</span> column1, column2,...] <span class="hljs-keyword">FROM</span> table_name[, table_name2,...]<br><span class="hljs-keyword">WHERE</span> [condition1 <span class="hljs-operator">|</span> expression1 ] [ <span class="hljs-keyword">AND</span> <span class="hljs-operator">|</span> <span class="hljs-keyword">OR</span> condition2 <span class="hljs-operator">|</span> expression2 ];<br></code></pre></td></tr></table></figure>

<h4 id="ORDER-BY子句"><a href="#ORDER-BY子句" class="headerlink" title="ORDER BY子句"></a>ORDER BY子句</h4><p>我们一般需要让输出以某种方式进行排序，为此可以使用ORDER BY子句，它能够以用户指定的列表格式对查询结果进行排列。ORDER BY子句的默认次序是升序，也就是说，如果对输出为字符的结果进行排序，就是A到Z的次序。反之，降序就是以Z到A的次序显示字符结果。对于数字值来说，升序是从1到9，降序是从9到1。<br>ORDER BY子句的语法是:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">SELECT</span> [ <span class="hljs-operator">*</span> <span class="hljs-operator">|</span><span class="hljs-keyword">ALL</span> <span class="hljs-operator">|</span> <span class="hljs-keyword">DISTINCT</span> column1, column2,...] <span class="hljs-keyword">FROM</span> table_name[, table_name2,...]<br><span class="hljs-keyword">WHERE</span> [condition1 <span class="hljs-operator">|</span> expression1 ] [ <span class="hljs-keyword">AND</span> <span class="hljs-operator">|</span> <span class="hljs-keyword">OR</span> condition2 <span class="hljs-operator">|</span> expression2 ]<br><span class="hljs-keyword">ORDER</span> <span class="hljs-keyword">BY</span> column1<span class="hljs-operator">|</span><span class="hljs-type">integer</span> [ <span class="hljs-keyword">ASC</span> <span class="hljs-operator">|</span> <span class="hljs-keyword">DESC</span>];<br></code></pre></td></tr></table></figure>
<p>在ORDER BY子句中，排序的方式</p>
<ul>
<li>指定字段名称</li>
<li>指定显示结果的列位置绝对索引(从1开始计数)，该方式存在一旦改变SELECT子句字段的顺序，ORDER BY的排序规则也需要进行相应的改动</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs sql">mysql<span class="hljs-operator">&gt;</span> <span class="hljs-keyword">select</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">from</span> sales.products_tbl <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> cost;<br><span class="hljs-operator">+</span><span class="hljs-comment">---------+-------------------------+-------+</span><br><span class="hljs-operator">|</span> prod_id <span class="hljs-operator">|</span> prod_desc               <span class="hljs-operator">|</span> cost  <span class="hljs-operator">|</span><br><span class="hljs-operator">+</span><span class="hljs-comment">---------+-------------------------+-------+</span><br><span class="hljs-operator">|</span> <span class="hljs-number">87</span>      <span class="hljs-operator">|</span> Plastic Spiders         <span class="hljs-operator">|</span>  <span class="hljs-number">1.05</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">13</span>      <span class="hljs-operator">|</span> <span class="hljs-literal">False</span> Paraffin Teeth    <span class="hljs-operator">|</span>  <span class="hljs-number">1.10</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">9</span>       <span class="hljs-operator">|</span> Candy Corn              <span class="hljs-operator">|</span>  <span class="hljs-number">1.35</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">6</span>       <span class="hljs-operator">|</span> Pumpkin Candy           <span class="hljs-operator">|</span>  <span class="hljs-number">1.45</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">119</span>     <span class="hljs-operator">|</span> assorted Masks          <span class="hljs-operator">|</span>  <span class="hljs-number">4.95</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">222</span>     <span class="hljs-operator">|</span> Plastic Pumpkin <span class="hljs-number">18</span> inch <span class="hljs-operator">|</span>  <span class="hljs-number">7.75</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">15</span>      <span class="hljs-operator">|</span> Assorted Costumes       <span class="hljs-operator">|</span> <span class="hljs-number">10.00</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">90</span>      <span class="hljs-operator">|</span> Lighted Lanterns        <span class="hljs-operator">|</span> <span class="hljs-number">14.50</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">11235</span>   <span class="hljs-operator">|</span> Witch Costume           <span class="hljs-operator">|</span> <span class="hljs-number">29.99</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">+</span><span class="hljs-comment">---------+-------------------------+-------+</span><br><br>mysql<span class="hljs-operator">&gt;</span> <span class="hljs-keyword">select</span> <span class="hljs-operator">*</span> <span class="hljs-keyword">from</span> sales.products_tbl <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> <span class="hljs-number">3</span>;<br><span class="hljs-operator">+</span><span class="hljs-comment">---------+-------------------------+-------+</span><br><span class="hljs-operator">|</span> prod_id <span class="hljs-operator">|</span> prod_desc               <span class="hljs-operator">|</span> cost  <span class="hljs-operator">|</span><br><span class="hljs-operator">+</span><span class="hljs-comment">---------+-------------------------+-------+</span><br><span class="hljs-operator">|</span> <span class="hljs-number">87</span>      <span class="hljs-operator">|</span> Plastic Spiders         <span class="hljs-operator">|</span>  <span class="hljs-number">1.05</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">13</span>      <span class="hljs-operator">|</span> <span class="hljs-literal">False</span> Paraffin Teeth    <span class="hljs-operator">|</span>  <span class="hljs-number">1.10</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">9</span>       <span class="hljs-operator">|</span> Candy Corn              <span class="hljs-operator">|</span>  <span class="hljs-number">1.35</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">6</span>       <span class="hljs-operator">|</span> Pumpkin Candy           <span class="hljs-operator">|</span>  <span class="hljs-number">1.45</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">119</span>     <span class="hljs-operator">|</span> assorted Masks          <span class="hljs-operator">|</span>  <span class="hljs-number">4.95</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">222</span>     <span class="hljs-operator">|</span> Plastic Pumpkin <span class="hljs-number">18</span> inch <span class="hljs-operator">|</span>  <span class="hljs-number">7.75</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">15</span>      <span class="hljs-operator">|</span> Assorted Costumes       <span class="hljs-operator">|</span> <span class="hljs-number">10.00</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">90</span>      <span class="hljs-operator">|</span> Lighted Lanterns        <span class="hljs-operator">|</span> <span class="hljs-number">14.50</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">|</span> <span class="hljs-number">11235</span>   <span class="hljs-operator">|</span> Witch Costume           <span class="hljs-operator">|</span> <span class="hljs-number">29.99</span> <span class="hljs-operator">|</span><br><span class="hljs-operator">+</span><span class="hljs-comment">---------+-------------------------+-------+</span><br><br># <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> <span class="hljs-number">3</span> 和 <span class="hljs-keyword">order</span> <span class="hljs-keyword">by</span> cost 是一个意思，<span class="hljs-number">3</span>是指字段索引<br></code></pre></td></tr></table></figure>

<h4 id="查询例子"><a href="#查询例子" class="headerlink" title="查询例子"></a>查询例子</h4><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">select</span> <span class="hljs-keyword">distinct</span> prod_desc <span class="hljs-keyword">from</span> sales.candy_tbl;<br><br><span class="hljs-keyword">select</span> <span class="hljs-keyword">distinct</span>(prod_desc) <span class="hljs-keyword">from</span> sales.candy_tbl;<br><br><span class="hljs-keyword">select</span> prod_desc <span class="hljs-keyword">as</span> <span class="hljs-string">&#x27;prod_desc的别名&#x27;</span> <span class="hljs-keyword">from</span> sales.candy_tbl;<br></code></pre></td></tr></table></figure>

<h3 id="字符大小写敏感性"><a href="#字符大小写敏感性" class="headerlink" title="字符大小写敏感性"></a>字符大小写敏感性</h3><p>在使用SQL编写代码时，大小写敏感性是一个需要理解的重要概念。一般来说，SQL命令和关键字是不区分大小写的，也就是允许我们以大写或小写来输入命令和关键字，而且可以混用。<br>对于MySQL来说通过lower_case_table_names的设置来表名的大小敏感，1&#x3D;表示大小写不敏感，0表示敏感</p>
<p>排序规则(gOLLATION)决定了RDBMS如何查找数据，包括排序方式和大小写敏感性等内容。数据的大小写敏感性很重要，这直接决定了WHERE子句如何匹配记录，在开发时用户务必要明确数据库在排序规则方面的相关规则</p>
<ul>
<li>Oracle，默认是大小写敏感的</li>
<li>MySQL和Microsoft SQL Server，默认是大小写不敏感的。这就意味着，在进行数据匹配时，系统会忽视数据的大小写。</li>
<li>MySQL可以通过server、database、table、column4个级别的CHARACTER SET和COLLAIION的排序规则，越小层次设置的字符集属性，优先级越大(server &lt; database &lt; table column)，即column级别的字符集设置会覆盖其他层次的字符集设置。</li>
</ul>
<p>MySQL相关命令</p>
<ul>
<li>查询数据库支持的字符集SHOW CHARACTER SET、SHOW CHAR SET、SHOW CHARSET常用的字符集有GBK、UTF8.<br>UTF8MB4</li>
<li>查询数据库支持的排序规则SHOW COLLATION</li>
</ul>
<p>COLLATION中一般以”_ci”, “_cs”,”_bin”结尾，其中</p>
<ul>
<li>ci表示case insensitive，字符串比较是大小写不敏感,a和A当做一样对待</li>
<li>cs表示case sensitive，字符串比较是大小写敏感,a和A会别区别对待</li>
<li>bin表示binary (二进制)，a和A会别区别对待</li>
</ul>
<blockquote>
<p>设置CHARACTER SET和COLLATION</p>
</blockquote>
<p>server，字符集默认使用utf8mb4; collation默认使用utf8mb4_0900_ai_ci，在server启动是可以修改</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs sql">mysql<span class="hljs-operator">&gt;</span> mysqld <span class="hljs-comment">--character-set-server=utf8 --collation-server=utf8_unicode_ci</span><br></code></pre></td></tr></table></figure>
<blockquote>
<p>database</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> DATABASE database_name <span class="hljs-type">CHARACTER</span> <span class="hljs-keyword">SET</span> utf8 COLLATEutf8_bin;<br><span class="hljs-keyword">ALTER</span> DATABASE database_name <span class="hljs-type">CHARACTER</span> <span class="hljs-keyword">SET</span> utf8 <span class="hljs-keyword">COLLATE</span> utf8_bin;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>table</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs sql"><span class="hljs-keyword">CREATE</span> <span class="hljs-keyword">TABLE</span> TABLE_NAME( …..)<br>ENGINE<span class="hljs-operator">=</span>InnoDB <span class="hljs-keyword">DEFAULT</span> CHARSET<span class="hljs-operator">=</span>utf8 <span class="hljs-keyword">COLLATE</span><span class="hljs-operator">=</span>utf8_bin;<br><span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">TABLE</span> TABLE_NAME <span class="hljs-keyword">CONVERT</span> <span class="hljs-keyword">TO</span> <span class="hljs-type">CHARACTER</span> <span class="hljs-keyword">SET</span> utf8 <span class="hljs-keyword">COLLATE</span> utf8_bin;<br></code></pre></td></tr></table></figure>
<blockquote>
<p>column</p>
</blockquote>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs sql">column_name [<span class="hljs-type">CHAR</span> <span class="hljs-operator">|</span> <span class="hljs-type">VARCHAR</span> │TEXT] (length)<span class="hljs-type">CHARACTER</span> <span class="hljs-keyword">SET</span>character_set_name <span class="hljs-keyword">COLLATE</span> collation_name<br><span class="hljs-keyword">ALTER</span> <span class="hljs-keyword">TABLE</span> TABLENAME MODIFY <span class="hljs-keyword">COLUMN</span> COLUMNNAME <span class="hljs-type">VARCHAR</span>(<span class="hljs-number">50</span>)<span class="hljs-type">BINARY</span> <span class="hljs-type">CHARACTER</span> <span class="hljs-keyword">SET</span> utf8 <span class="hljs-keyword">COLLATE</span> utf8_bin <span class="hljs-keyword">DEFAULT</span> <span class="hljs-keyword">NULL</span>;<br></code></pre></td></tr></table></figure>




                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/categories/%E5%A4%A7%E6%95%B0%E6%8D%AE/" class="category-chain-item">大数据</a>
  
  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/tags/%E5%A4%A7%E6%95%B0%E6%8D%AE/">#大数据</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>DB08_数据库查询</div>
      <div>http://example.com/2022/12/22/数据库/MySQL/大数据学习/DB08&amp;DB09/DB08_数据库查询/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>深海火锅店</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2022年12月22日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2022/12/22/%E5%85%B6%E4%BB%96%E6%8A%80%E6%9C%AF%E6%A0%88/%E5%89%AA%E8%BE%91/PS/PS%E5%BC%82%E9%97%BB%E5%BD%95/01_%E8%AE%A4%E8%AF%86%E7%95%8C%E9%9D%A2/" title="认识PS界面">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">认识PS界面</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2022/12/22/%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%AD%A6%E4%B9%A0/DB08&amp;DB09/DB09_sql%E6%93%8D%E4%BD%9C%E7%AC%A6/" title="DB09_SQL操作符">
                        <span class="hidden-mobile">DB09_SQL操作符</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
       <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="busuanzi_container_site_pv" style="display: none">
        总访问量 
        <span id="busuanzi_value_site_pv"></span>
         次
      </span>
    
    
      <span id="busuanzi_container_site_uv" style="display: none">
        总访客数 
        <span id="busuanzi_value_site_uv"></span>
         人
      </span>
    
    
  
</div>

  
  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script  src="/js/local-search.js" ></script>

  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  
<script src="//cdn.jsdelivr.net/gh/EmoryHuang/BlogBeautify@1.1/DynamicRibbon.min.js"></script>



<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>
</body>
</html>
